package com.codemart.lanze.modules.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codemart.lanze.modules.sys.mapper.PermissionMapper;
import com.codemart.lanze.modules.sys.mapper.RolePermissionMapper;
import com.codemart.lanze.modules.sys.model.Permission;
import com.codemart.lanze.modules.sys.model.RolePermission;
import com.codemart.lanze.modules.sys.vo.TreeNode;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * @description:
 * @author: lanze
 */
@Transactional
@Service
public class PermissionService extends ServiceImpl<PermissionMapper, Permission> {

	@Resource
	private PermissionMapper permissionMapper;
	@Resource
	private RolePermissionMapper rolePermissionMapper;

	/**
	 * 根据用户ID查询用户菜单列表
	 *
	 * @param UserId
	 * @return
	 */
	@Transactional(readOnly = true)
	public IPage<Permission> findPage(Integer pageNum, Integer pageSize, String name) {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

		if (StringUtils.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		IPage<Permission> iPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
		return iPage;
	}

	/**
	 * 根据用户ID查询该用户所拥有的权限
	 *
	 * @param UserId
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Permission> findListPermissionByUserId(Long userId) {
		return permissionMapper.findListPermissionByUserId(userId);
	}

	/**
	 * 根据用户ID查询用户菜单列表
	 *
	 * @param UserId
	 * @return
	 */
	// @Cacheable(value = "menuListCache", key = "#userId")
	@Transactional(readOnly = true)
	public List<Permission> findMenuListByUserId(Long userId) {
		return permissionMapper.findMenuListByUserId(userId);
	}

	/**
	 * 根据资源类型查询权限列表
	 *
	 * @param type
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Permission> findListByType(String type) {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

		if (StringUtils.isNotEmpty(type)) {
			queryWrapper.eq("type", type);
		}
		return this.list(queryWrapper);
	}

	/**
	 * 返回树列表
	 *
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<TreeNode> findTreeList() {
		return permissionMapper.findTreeList();
	}

	/**
	 * 根据权限ID删除权限并级联删除权限和角色的关联信息
	 *
	 * @param permissionId
	 * @return
	 */
	public Boolean deletePermissionAndRolePermissionByPermissionId(Long permissionId) {

		// 删除该权限和角色的关联信息
		QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("permission_id", permissionId);
		rolePermissionMapper.delete(queryWrapper);

		// 删除权限
		boolean count1 = this.removeById(permissionId);
		return count1;
	}

	/**
	 * 根据资源名称模糊匹配
	 *
	 * @param menuName
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Permission> findListByMenuName(String menuName) {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

		if (StrUtil.isNotEmpty(menuName)) {
			queryWrapper.like("name", menuName);
		}
		return this.list(queryWrapper);
	}
}
